package com.ww.java8.fp.stream.inaction.ch05;

import java.util.Arrays;
import java.util.List;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import com.ww.java8.fp.stream.inaction.ch04.Dish;

public class NumericStreams {

	public static void main(String[] args) {
		List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);
		Arrays.stream(numbers.toArray()).forEach(System.out::println);
		
		/**
		 * mapToInt
		 */
		int calories = Dish.menu.stream()
			.mapToInt(Dish::getCalories)
			.sum();
		System.out.println("Number of calories:" + calories);
		
		OptionalInt maxCalories = Dish.menu.stream()
			.mapToInt(Dish::getCalories)
			.max();
		int max;
		if (maxCalories.isPresent()) {
			max = maxCalories.getAsInt();
		} else {
			max = 1;
		}
		System.out.println(max);
		
		IntStream evenNumbers = IntStream.rangeClosed(1, 100)
			.filter(n -> 0 == n % 2);
		System.out.println(evenNumbers.count());
//		evenNumbers.forEach(System.out::println);
		
		Stream<int[]> pythagoreanTriples = IntStream.rangeClosed(1, 100)
			.boxed()
			.flatMap(a -> IntStream.range(a, 100)
					.filter(b -> 0 == Math.sqrt(a * a + b * b) % 1)
					.boxed()
					.map(b -> new int[] { a, b, (int) Math.sqrt(a * a + b * b) }));
		pythagoreanTriples.forEach(t -> System.out.println(t[0] + ", " + t[1] + ", " + t[2]));
	}
}
